﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Model
{
    public static class TestHelper
    {
        public static string ToJson(this object obj)
        {
            return LitJson.JsonMapper.ToJson(obj);
        }

        public static string ToHex(this byte[] bytes)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                builder.AppendFormat("{0:X2}", bytes[i]);
                if (i != bytes.Length - 1)
                    builder.Append(" ");
            }
            return builder.ToString();
        }

        public static string ToHex(this AMessage message)
        {
            OpcodeTypeComponent opcodeTypeComponent = Game.Scene.GetComponent<OpcodeTypeComponent>();
            Opcode opcode = opcodeTypeComponent.GetOpcode(message.GetType());

            ushort op = (ushort)opcode;
            byte[] tempOp = BitConverter.GetBytes(op);
            byte[] temp = ProtobufHelper.ToBytes(message);
            byte[] size = BitConverter.GetBytes((ushort)(tempOp.Length + temp.Length));
            return $"[{size.ToHex()} {tempOp.ToHex()} {temp.ToHex()}]";
        }

        public static string ToHex2(this AMessage message)
        {
            OpcodeTypeComponent opcodeTypeComponent = Game.Scene.GetComponent<OpcodeTypeComponent>();
            Opcode opcode = opcodeTypeComponent.GetOpcode(message.GetType());

            ushort op = (ushort)opcode;
            byte[] tempOp = BitConverter.GetBytes(op);
            byte[] temp = ProtobufHelper.ToBytes(message);
            byte[] size = BitConverter.GetBytes((ushort)(tempOp.Length + temp.Length));
            return $"{size.ToHex()} {tempOp.ToHex()} {temp.ToHex()}";
        }


    }
}
